﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <vector>
#include <algorithm>
using namespace std;

//1.一元谓词；
class GreaterThan20
{
public:
	bool operator()(int val)
	{
		return val > 20;
	}
};
//4.二元谓词；
class MyCompareClass
{
public:
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
};
void testPredicate()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	v.push_back(50);

	//2.查找第一个大于20的数字；
	vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterThan20());
	//3.第三个参数，函数对象，匿名对象；
	//GreaterThan20 greaterThan;
	//find_if(v.begin(), v.end(), greaterThan);

	if (pos != v.end())
	{
		cout << "找到大于20的数字为：" << *pos << endl;//30
	}
	else
	{
		cout << "未找到！" << endl;
	}

	sort(v.begin(), v.end(), MyCompareClass());
	//5.匿名函数，lambda表达式，[](){};
	for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });//50 40 30 20 10
}

//main331：谓词的用法
int main331()
{
	testPredicate();

	system("pause");
	return EXIT_SUCCESS;
}